#!/bin/bash

set -x
set -e

# 优化，仅优化编译已有的参数、OpenMP 等
# 使用 DPC/C++
CompileMode=optparamsdpc

ROOT_PATH=`cd $(dirname $0) && pwd`
SCRIPT_NAME=`basename $0`
SCRIPT=$ROOT_PATH/`basename $0`
cd ${ROOT_PATH}

module purge
module load compiler/2023.1.0
module load openmpi/4.1.6-mlnxofed-cuda12.3-gdrcopy-full
module load nvhpc-nompi/23.11
module load mkl/2023.1.0
module load cmake/3.22
export OMPI_CC=icx
export OMPI_CXX=icpx
export OMPI_FC=ifx

mkdir -p ${ROOT_PATH}/softwares

# lapack
cd ${ROOT_PATH}/lapack-3.11
sed -i 's#CC = .*#CC = gcc#g' make.inc
sed -i 's#FC = .*#FC = gfortran#g' make.inc
make blaslib -j 16
make cblaslib -j 16
make lapacklib -j 16
make lapackelib -j 16

# parmetis
cd ${ROOT_PATH}/parmetis-4.0.3
## make config cc=mpiicc prefix=/es01/paratera/sce0588/zl/ASC/parmetis-4.0.3/parmetis-install
make config cc=mpicc openmp=1 prefix=${ROOT_PATH}/softwares/parmetis/4.0.3/${CompileMode}
make -j 16
make install

# hypre
cd ${ROOT_PATH}/hypre-2.28.0/src
## configure
CC=mpicc CFLAGS+="-fPIC -march=native -mtune=native -O3" CXX=mpic++ CXXFLAGS+="-fPIC -march=native -mtune=native -O3" FC=mpifort FCFLAG+="-fPIC -march=native -mtune=native -O3" CUCC="nvcc" CUFLAGS+="-ccbin icpx -Xcompiler='-fPIC -march=native -mtune=native -O3'" ./configure --prefix="${ROOT_PATH}/softwares/hypre/2.28.0/${CompileMode}" --with-MPI --enable-shared
## compile
make -j 16
## install
make install

# petsc
cd ${ROOT_PATH}/petsc-3.19.3

export PETSC_DIR=${ROOT_PATH}/petsc-3.19.3
export PETSC_ARCH=x86_64

./configure CC=mpicc CXX=mpic++ FC=mpifort CFLAGS+="-march=native -mtune=native -O3" CXXFLAGS+="-march=native -mtune=native -O3" FFLAGS="-march=native -mtune=native -O3" \
    --prefix="${ROOT_PATH}/softwares/petsc/3.19.3/${CompileMode}" \
    --with-fortran-bindings=0 \
	--with-hypre-dir="${ROOT_PATH}/softwares/hypre/2.28.0/${CompileMode}" \
    --with-debugging=0 \
    --with-openmp=1

make -j 20 PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} all
make all check

# petsc_solver
cd ${ROOT_PATH}/petsc_solver

export CC=mpicc
export CXX=mpic++
export CPATH=${ROOT_PATH}/lapack-3.11/CBLAS/include:${ROOT_PATH}/lapack-3.11/LAPACKE/include:$CPATH

# export LD_LIBRARY_PATH=${ROOT_PATH}/lapack-3.11:$LD_LIBRARY_PATH
sed -i "s#set(PETSC_DIR .*)#set(PETSC_DIR ${ROOT_PATH}/petsc-3.19.3)#g" CMakeLists.txt
sed -i "s#set(PETSC_ARCH .*)#set(PETSC_ARCH ${PETSC_ARCH})#g" CMakeLists.txt

rm -rf build
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
mv libpetsc_solver.a ../lib/

# OpenCAEPoro
cd ${ROOT_PATH}/OpenCAEPoro

export CC=mpicc
export CXX=mpic++

## users specific directory paths
export PARMETIS_DIR=${ROOT_PATH}/parmetis-4.0.3
export PARMETIS_BUILD_DIR=${PARMETIS_DIR}/build/Linux-x86_64
export METIS_DIR=${ROOT_PATH}/parmetis-4.0.3/metis
export METIS_BUILD_DIR=${PARMETIS_DIR}/build/Linux-x86_64
export PETSC_DIR=${ROOT_PATH}/petsc-3.19.3
export PETSC_ARCH=${PETSC_ARCH}
export PETSCSOLVER_DIR=${ROOT_PATH}/petsc_solver
export CPATH=${ROOT_PATH}/petsc-3.19.3/include/:$CPATH
export CPATH=${ROOT_PATH}/petsc-3.19.3/${PETSC_ARCH}/include/:${ROOT_PATH}/parmetis-4.0.3/metis/include:${ROOT_PATH}/parmetis-4.0.3/include:$CPATH
export CPATH=${ROOT_PATH}/lapack-3.11/CBLAS/include/:$CPATH

## install
rm -rf build; mkdir build; cd build;

echo "cmake -DUSE_PETSCSOLVER=ON -DUSE_PARMETIS=ON -DUSE_METIS=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=ON .."
cmake -DUSE_PETSCSOLVER=ON -DUSE_PARMETIS=ON -DUSE_METIS=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=ON ..

echo "make -j 32"
make -j 32

echo "make install"
make install

# link OpenCAEPoro
mkdir -p ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/lib
mkdir -p ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/data
cp ${ROOT_PATH}/OpenCAEPoro/lib/libOpenCAEPoroX.a ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/lib/
cp ${ROOT_PATH}/OpenCAEPoro/testOpenCAEPoro ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/
cp -r ${ROOT_PATH}/OpenCAEPoro/data/* ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/data